package net.zigin.nhi.could.waste.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.annotation.EpiResponseFilter;
import net.zigin.nhi.could.waste.annotation.EpiResponseFilters;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.HospitalBaseDto;
import net.zigin.nhi.could.waste.model.HospitalBase;
import net.zigin.nhi.could.waste.service.HospitalBaseService;
import net.zigin.nhi.could.waste.service.SysUserHospitalService;
import net.zigin.nhi.could.waste.util.HttpClientUtil;
import net.zigin.nhi.could.waste.util.OSSClientUtil;
import net.zigin.nhi.could.waste.vo.AreaProvinceVo;
import net.zigin.nhi.could.waste.vo.HospitalBaseVo;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HospitalBase控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: HospitalBaseController
 * @date 2021-9-8 0:01:02
 */
@Api(tags = "医院管理")
@RestController
@RequestMapping("/hospitalBase")
public class HospitalBaseController extends BaseController {
    @Autowired
    private HospitalBaseService hospitalBaseService;
    @Autowired
    private SysUserHospitalService sysUserHospitalService;
    @Autowired
    private OSSClientUtil ossClientUtil;

    /**
     * 新增编辑医院
     *
     * @param hospitalBaseDto 医院信息
     * @return 医院ID
     */
    @ApiOperation("新增编辑医院")
    @ApiImplicitParam(name = "hospitalBaseDto", value = "医院信息", required = true, dataType = "HospitalBaseDto")
    @PostMapping("/save")
    public JsonForResult<String> sava(@RequestBody HospitalBaseDto hospitalBaseDto) {
        String id = hospitalBaseService.saveOrUpdate(hospitalBaseDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 医院列表
     *
     * @param hospitalBaseDto 医院信息
     * @return 医院列表
     */
    @ApiOperation("医院列表")
    @ApiImplicitParam(name = "hospitalBaseDto", value = "医院信息", required = true, dataType = "HospitalBaseDto")
    @PostMapping("/getList")
    public JsonForResult<List<HospitalBaseVo>> getList(@RequestBody HospitalBaseDto hospitalBaseDto) {
        List<HospitalBaseVo> list = hospitalBaseService.getList(hospitalBaseDto);
        Integer count = hospitalBaseService.getCount(hospitalBaseDto);
        return JsonForResult.<List<HospitalBaseVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 医院详情
     *
     * @param id 医院ID
     * @return 医院详情
     */
    @ApiOperation("医院详情")
    @ApiImplicitParam(name = "id", value = "医院id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<HospitalBaseVo> getById(@PathVariable String id) {
        HospitalBase hospitalBase = hospitalBaseService.selectByPrimaryKey(id);
        HospitalBaseVo hospitalBaseVo = ReflectUtil.copyProperties(hospitalBase, HospitalBaseVo.class);
        hospitalBaseVo.setAdminId(sysUserHospitalService.getSysUserIdByHospitalBaseId(hospitalBaseVo.getId()));
        return JsonForResult.<HospitalBaseVo>builder().data(hospitalBaseVo).success(true).build();
    }

    /**
     * 删除医院
     *
     * @param id 医院ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除医院")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        hospitalBaseService.deleteById(id);
        return JsonForResult.builder().success(true).build();
    }

    @LoginWhitePathAnnotation
    @PostMapping("testoss")
    public String headImgUpload(HttpServletRequest request, MultipartFile file) {
        HttpClientUtil.smsCustomCode("https://jiangxi.zigin.net/api/msg/message/sendCustomMessage","18714363098","早点睡！");
        Map<String, Object> value = new HashMap<String, Object>();
        try {
            String url = ossClientUtil.updateHead(file);
            value.put("data", url);
            value.put("code", 0);
            value.put("msg", "图片上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            value.put("code", 2000);
            value.put("msg", "图片上传成功");
        }
        return JSONObject.toJSONString(value);
    }

    @LoginWhitePathAnnotation
    @ApiOperation("医院注册")
    @ApiImplicitParam(name = "hospitalBaseDto", value = "医院信息", required = true, dataType = "HospitalBaseDto")
    @PostMapping("/register")
    public JsonForResult register(@RequestBody HospitalBaseDto hospitalBaseDto, HttpSession session) {
        Object code = session.getAttribute(hospitalBaseDto.getAdminMobile());
        if (code != null && hospitalBaseDto.getSmsCode().equals(code.toString())) {
            String id = hospitalBaseService.register(hospitalBaseDto);
            return JsonForResult.builder().data(id).success(true).build();
        } else {
            return JsonForResult.builder().message("验证码不正确").success(false).build();
        }

    }


}
